package com.wxzz.elearning.common.bean;

import cn.hutool.core.util.StrUtil;
import com.wxzz.elearning.common.jgenum.StatusCodeEnum;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;

public class ResponseData<T> {

    private static final String MES_KEY_PRE_CN = "msg_cn";
    private static final String MES_KEY_PRE_EN = "msg_en";

    // 返回码，类似HTTP响应码。如：200成功，500服务器错误，404资源不存在等。
    private Integer code;
    // 业务数据
    private T data;
    // 返回描述
    private String msg;

    private String msg_en;
    private String msgCode;


    // 身份标识， JWT生成的令牌。
    private String token;

    public ResponseData() {
    }

    private void init(String msgCode,RedisTemplate redisTemplate,String[] formats){
        this.msgCode = msgCode;
        this.code = StatusCodeEnum.SUCCESS_CODE.getStatusCode();
        //获取msg的值
        HashOperations hashOperations = redisTemplate.opsForHash();
        Object messageValueCN = hashOperations.get(MES_KEY_PRE_CN, msgCode);
        Object messageValueEN = hashOperations.get(MES_KEY_PRE_EN, msgCode);

        if(null == messageValueCN || "".equals(messageValueCN.toString())){
            this.msg = msgCode;
        }else{
            if(formats!=null&&formats.length>0){
                this.msg = StrUtil.format(messageValueCN.toString(),formats);
            }else{
                this.msg = messageValueCN + "";
            }
        }

        if(null == messageValueEN || "".equals(messageValueEN.toString())){
            this.msg_en = msgCode;
        }else{
            if(formats!=null&&formats.length>0){
                this.msg_en = StrUtil.format(messageValueEN.toString(),formats);
            }else{
                this.msg_en = messageValueEN + "";
            }
        }
    }

    public ResponseData(T data) {
        this.code = StatusCodeEnum.SUCCESS_CODE.getStatusCode();
        this.data = data;
    }

    public ResponseData(String msgCode,RedisTemplate redisTemplate) {
        init(msgCode,redisTemplate,null);
    }


    public ResponseData(String msgCode,RedisTemplate redisTemplate,String[] formats) {
        init(msgCode,redisTemplate,formats);
    }



    public ResponseData(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public ResponseData(String msgCode, T data, RedisTemplate redisTemplate) {
        init(msgCode,redisTemplate,null);
        this.data = data;
    }
    public ResponseData(String msgCode, T data, RedisTemplate redisTemplate,String[] formats) {
        init(msgCode,redisTemplate,formats);
        this.data = data;
    }


    public ResponseData(Integer code, T data, String msg) {
        this.code = code;
        this.data = data;
        this.msg = msg;
    }

    public ResponseData(String msgCode, T data, RedisTemplate redisTemplate, String token,String[] formats) {
        init(msgCode,redisTemplate,formats);
        this.data = data;
        this.token = token;
    }

    public ResponseData(String msgCode, T data, RedisTemplate redisTemplate, String token) {
        init(msgCode,redisTemplate,null);
        this.data = data;
        this.token = token;
    }

    public ResponseData(Integer code, T data, String msg, String token) {
        this.code = code;
        this.data = data;
        this.msg = msg;
        this.token = token;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public String getMsg_en() {
        return msg_en;
    }

    public void setMsg_en(String msg_en) {
        this.msg_en = msg_en;
    }

    public String getMsgCode() {
        return msgCode;
    }

    public void setMsgCode(String msgCode) {
        this.msgCode = msgCode;
    }
}

